package com.tjetc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.entity.Activity;
import com.tjetc.entity.ActivityType;
import com.tjetc.entity.Admin;
import com.tjetc.entity.Tribe;
import com.tjetc.mapper.ActivityMapper;
import com.tjetc.service.IActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjetc.service.IActivityTypeService;
import com.tjetc.service.ITribeService;
import com.tjetc.utils.MinioUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    @Resource
    private ActivityMapper activityMapper;
    @Autowired
    private IActivityTypeService activityTypeService;

    @Autowired
    private ITribeService tribeService;

    @Autowired
    private MinioUtils minioUtils;

    @Override
    public PageInfo<Activity> lists(Integer curPage, Integer pageSize, Integer typeId, String name, Integer tribeId,Integer applyId) {
        PageHelper.startPage(curPage, pageSize);
        List<Activity> list=activityMapper.lists(name,typeId,tribeId,applyId);
        if (list.size()>0){

            for (Activity activity : list) {
                ActivityType activityType=activityTypeService.getById(activity.getTypeId());
                activity.setActivityType(activityType);
                Tribe tribe=tribeService.getById(activity.getTribeId());
                activity.setTribe(tribe);
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Activity activity, MultipartFile photo) {
        if (photo!=null){
            String path = minioUtils.upload(photo);
            activity.setPhotopath(path);
        }
        activityMapper.insert(activity);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updates(Activity activity, MultipartFile photo) {
        if (photo!=null){
            String path = minioUtils.upload(photo);
            activity.setPhotopath(path);
        }
        activityMapper.updateById(activity);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(Integer id) {
        Activity activity=activityMapper.selectById(id);
        activity.setStatus(1);
        activityMapper.updateById(activity);
        return true;
    }
}
